// JavaScript Document

//退出modal清除表单缓存
$("#myModal").on("hide.bs.modal", function () {
    var Ilength = $("#dataForm input").length;
    for (var i = 0; i < Ilength - 1; i++) {
        $("#dataForm input")[i].value = ""
    }
});

//确认是否提交入住信息
$("#handIn").on("click", function () {
    var name = $("#dataForm input")[0].value;
    var idCard = $("#dataForm input")[1].value;
    var tel = $("#dataForm input")[2].value;
    var roomId = $("#dataForm input")[3].value;
    var status = confirm("姓名 :" + name + "\n" + "身份证 :" + idCard + "\n" + "电话 :" + tel + "\n" + "房间编号 :" + roomId);
    if (status) {
        var sendInfo = "name=" + name + "&idCard=" + idCard + "&tel=" + tel + "&roomId=" + roomId;
        sendDdtaforpost("http://localhost:8080/bookRoom.action", encodeURI(sendInfo), function (data) {
            console.log(data);
            if (data === "GOOD") {
                alert("操作成功！！！");
                showAllRoominfo();
            } else if (data === "FAILED") {
                alert("入住信息录入错误");
            }
        });
        $("#myModal").modal("hide");
    } else if (!status) {

    }
});

//确认退房信息
$("#checkOutbtn").on("click", function () {
    var checkOutinfo = $("#checkOut span");
    var name = checkOutinfo[0].innerHTML;
    var IDcard = checkOutinfo[1].innerHTML;
    var tel = checkOutinfo[2].innerHTML;
    var roomId = checkOutinfo[3].innerHTML;
    var inTime = checkOutinfo[4].innerHTML;


    console.log("Get roomId : " + roomId);
    var status = confirm("姓名 :" + name + "\n" + "身份证 :" + IDcard + "\n" + "电话 :" + tel + "\n" + "房间编号 :" + roomId + "\n" + "入住时间 :" + inTime);
    if (status) {
        sendDdtaforpost("http://localhost:8080/checkOut.action", encodeURI(roomId), function (data) {
            console.log(data);
            if (data === "GOOD") {
                alert("退房成功！！！");
                showAllRoominfo();
            } else if (data === "FAILED") {
                alert("退房失败！！！");
            }
        });
        $("#checkOut").modal("hide");
    } else if (!status) {

    }
});


//将对应房间号传递给modal
function show(roomId) {
    $("#roomId")[0].value = roomId;
};

//将退房房间号传递给modal
function checkOutshow(roomId) {
    $("#checkOut span")[3].innerHTML = roomId;
    sendDdtaforpost("http://localhost:8080/find_bookRoom_info.action", encodeURI("roomId=" + roomId), function (data) {
        data = JSON.parse(data);
        var checkOutinfo = $("#checkOut span");
        checkOutinfo[0].innerHTML = data[0].hotelClientName;
        checkOutinfo[1].innerHTML = data[0].hotelClientIdcard;
        checkOutinfo[2].innerHTML = data[0].hotelClientTel;
        checkOutinfo[3].innerHTML = data[0].roomId;
        checkOutinfo[4].innerHTML = data[0].hotelInTime;
    });
}

//将入住房间编号传递给modal
function updateInfo(roomId,name,idcard,tel,inTime) {
    var updateInSpan = $("#updateIn input");
    updateInSpan[0].value = roomId;
    updateInSpan[1].value = name;
    updateInSpan[2].value = idcard;
    updateInSpan[3].value = tel;
    updateInSpan[4].value = inTime;
    updateInSpan[5].value = roomId;
}

//确认修改信息
$("#updateCheck").on("click",function () {
    var updateInSpan = $("#updateIn input");
    var roomId = updateInSpan[0].value;
    var name = updateInSpan[1].value;
    var idcard = updateInSpan[2].value;
    var tel = updateInSpan[3].value;
    var inTime = updateInSpan[4].value;
    var oldroomId = updateInSpan[5].value;
    var queryString = "oldRoomid="+oldroomId+"&roomId="+roomId+"&name="+name+"&idCard="+idcard+"&tel="+tel+"&inTime="+inTime;
    var status = confirm("房间编号："+roomId+"\n" +
            "姓名："+name+"\n" +
            "身份证号"+idcard+"\n" +
            "电话："+tel+"\n" +
            "入住时间："+inTime+"\n");
    if(status){
        sendDdtaforpost("http://localhsot:8080/updatePeopleinfo.action",encodeURI(queryString),function (data) {
           if(data==="GOOD"){
               alert("更新成功！！！");
           } else if(data==="FAILED"){
               alert("更新失败！！！");
           }
        });
    }
});

function showAllRoominfo() {
    requestData("http://localhost:8080/allRoominfo.action", function (data) {
        var contentarea = "";
        data = JSON.parse(data);
        for (var i = 0; i < data.length; i++) {
            var status = "";
            var operation = "";
            if (!data[i].roomStatus) {
                status = "空";
                operation = " <td> <a  class='btn btn-primary' data-target='#myModal' data-toggle='modal' onclick='show("+data[i].roomId+")'>入住</a> </td>";
            } else {
                status = "满";
                operation = " <td> <a class='btn btn-danger' data-toggle='modal' data-target='#checkOut'  onclick='checkOutshow("+data[i].roomId+")'>退房</a> </td>";

            }

            var starthtml = "<tr>";
            var star = "<td>" + data[i].roomId + "</td>" +
                "<td>" + data[i].roomLevel + "</td>" +
                "<td>" + data[i].roomType + "</td>" +
                "<td>" + data[i].roomPrice + "</td>" +
                "<td>" + status + "</td>" +
                operation;
            var endhtml = "</tr>";
            contentarea += starthtml + star + endhtml;
        }
        document.getElementById("user").innerHTML = contentarea;
    });
}


//ajax Operation
function sendDdtaforpost(url, data, recall) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            recall(xmlhttp.responseText);
        }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
    xmlhttp.send(data);
}

function requestData(url, recall) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            recall(xmlhttp.responseText);
        }
    };
    xmlhttp.open('GET', url, true);
    xmlhttp.send();
}